package wetogether.travelinfo.control;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import wetogether.travelinfo.service.DeleteTravelInfoService;
import wetogether.travelinfo.vo.TravelCommentVO;
import wetogether.travelinfo.vo.TravelFrVO;
import wetogether.travelinfo.vo.TravelImageVO;
import wetogether.travelinfo.vo.TravelInfoVO;
import wetogether.travelinfo.vo.TravelVacancyVO;

public class DeleteTravelInfoController extends MultiActionController{
	private DeleteTravelInfoService deleteTravelInfoService;
	public void setDeleteTravelInfoService(DeleteTravelInfoService deleteTravelInfoService){
		this.deleteTravelInfoService=deleteTravelInfoService;
		System.out.println("TravelInfo_DeleteTravelInfoService injection....");
	}
	/**
	 * 1. 여행지 모두 삭제
	 */
	public ModelAndView deleteTravelInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infodelete_result");
		String place=request.getParameter("place");
		try{
			deleteTravelInfoService.deleteTravelInfo(place);
			mv.addObject("delete",
					place+" 여행지 delete OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 2. 이미지 하나씩 삭제
	 */
	public ModelAndView deletePlaceImage(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infodelete_result");
		TravelImageVO imgVO=new TravelImageVO();
		bind(request,imgVO);
		try{
			deleteTravelInfoService.deletePlaceImage(imgVO);
			mv.addObject("delete",
					imgVO.getPlace()+"의 "+imgVO.getGallery()+" delete OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 3. 숙박 하나씩 삭제
	 */
	public ModelAndView deletePlaceVacancy(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infodelete_result");
		TravelVacancyVO vacancyVO=new TravelVacancyVO();
		bind(request,vacancyVO);
		try{
			deleteTravelInfoService.deletePlaceVacancy(vacancyVO);
			mv.addObject("delete",
					vacancyVO.getPlace()+"의 "+vacancyVO.getStay()+" delete OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 4. 맛집 하나씩 삭제
	 */
	public ModelAndView deletePlaceFr(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infodelete_result");
		TravelFrVO frVO=new TravelFrVO();
		bind(request,frVO);
		try{
			deleteTravelInfoService.deletePlaceFr(frVO);
			mv.addObject("delete",
					frVO.getPlace()+"의 "+frVO.getRestaurant()+" delete OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 5. 공지사항 삭제
	 */
	public ModelAndView deletePlaceNotice(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infodelete_result");
		TravelInfoVO tVO=new TravelInfoVO();
		tVO.setPlace(request.getParameter("place"));
		// 공지는 TravelInfoVO에 포함되어있으므로 삭제 시 DB에 null값을 넣어준다.
		tVO.setNotice("");
		try{
			deleteTravelInfoService.deletePlaceNotice(tVO);
			mv.addObject("delete",
					tVO.getPlace()+"의 공지사항 delete OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 6. 댓글 하나씩 삭제
	 */
	public ModelAndView deletePlaceComment(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infodelete_result");
		TravelCommentVO commentVO=new TravelCommentVO();
		commentVO.setPlace(request.getParameter("place"));
		commentVO.setMemberid(request.getParameter("memberid"));
		commentVO.setRecommend(request.getParameter("recommend"));
		try{
			deleteTravelInfoService.deletePlaceComment(commentVO);
			// 댓글을 삭제할 때 댓글 입력시 넣었던 추천점수를 함께 지워준다.
			deleteTravelInfoService.deleteTravelInfoRecommend(commentVO);
			mv.addObject("delete",
					commentVO.getPlace()+"의 "+commentVO.getMemberid()+"님 댓글 delete OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
}
